package com.pin.zheng.xingmulibrary.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.pin.zheng.xingmulibrary.PinZhengBase;

import java.util.ArrayList;
import java.util.List;

public class SpUtil {
    private static SpUtil uniqueInstance = null;

    CustomSharedPreferences sp;

    private SpUtil() {
        // Exists only to defeat instantiation.
        sp = new CustomSharedPreferences(PinZhengBase.getContext(), PinZhengBase.getSpFileName());
//        sp = PinZhengBase.getContext().getSharedPreferences(PinZhengBase.getSpFileName(), Context.MODE_PRIVATE);
    }

    public static SpUtil getInstance() {
        if (uniqueInstance == null) {
            uniqueInstance = new SpUtil();
        }
        return uniqueInstance;
    }

    public void savaLong(String key, long value) {
//        SharedPreferences sp = PreferenceManager
//                .getDefaultSharedPreferences(MyApp.getApplication());
        sp.edit().putLong(key, value).apply();
    }

    public long getLong(String key) {
//        SharedPreferences sp = PreferenceManager
//                .getDefaultSharedPreferences(MyApp.getApplication());
        return sp.getLong(key, 0);
    }

    public void savaInteger(String key, int value) {
//        SharedPreferences sp = PreferenceManager
//                .getDefaultSharedPreferences(MyApp.getApplication());
        sp.edit().putInt(key, value).apply();
    }

    public int getInteger(String key) {
//        SharedPreferences sp = PreferenceManager
//                .getDefaultSharedPreferences(MyApp.getApplication());
        return sp.getInt(key, 0);
    }

    public int getInteger(String key, int value) {
//        SharedPreferences sp = PreferenceManager
//                .getDefaultSharedPreferences(MyApp.getApplication());
        return sp.getInt(key, value);
    }

    public void savaBoolean(String key, boolean value) {
//        SharedPreferences sp = PreferenceManager
//                .getDefaultSharedPreferences(MyApp.getApplication());
        sp.edit().putBoolean(key, value).apply();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public String getString(String key) {
//        SharedPreferences sp = PreferenceManager
//                .getDefaultSharedPreferences(MyApp.getApplication());
        return sp.getString(key, "");
    }

    public void remove(String key) {
//        SharedPreferences sp = PreferenceManager
//                .getDefaultSharedPreferences(MyApp.getApplication());
        sp.edit().remove(key).apply();
    }

    public void savaString(String key, String value) {
//        SharedPreferences sp = PreferenceManager
//                .getDefaultSharedPreferences(MyApp.getApplication());
        sp.edit().putString(key, value).apply();
    }

    public boolean getBoolean(String key) {
//        SharedPreferences sp = PreferenceManager
//                .getDefaultSharedPreferences(MyApp.getApplication());
        return sp.getBoolean(key, false);
    }

    /**
     * 用于保存集合
     *
     * @param key  key
     * @param list 集合数据
     * @return 保存结果
     */
    public <T> boolean putListData(String key, List<T> list) {
//        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MyApp.getApplication());
        SharedPreferences.Editor editor = sp.edit();
        if (list.size() == 0) {
            editor.putString(key, "");
            editor.apply();
            return true;
        }
        boolean result;
        String type = list.get(0).getClass().getSimpleName();

        JsonArray array = new JsonArray();
        try {
            switch (type) {
                case "Boolean":
                    for (int i = 0; i < list.size(); i++) {
                        if (!array.contains((JsonElement) list.get(i))) {
                            array.add((Boolean) list.get(i));
                        }
                    }
                    break;
                case "Long":
                    for (int i = 0; i < list.size(); i++) {
                        if (!array.contains((JsonElement) list.get(i))) {
                            array.add((Long) list.get(i));
                        }
                    }
                    break;
                case "Float":
                    for (int i = 0; i < list.size(); i++) {
                        if (!array.contains((JsonElement) list.get(i))) {
                            array.add((Float) list.get(i));
                        }
                    }
                    break;
                case "String":
                    for (int i = 0; i < list.size(); i++) {
                        if (!array.contains((JsonElement) list.get(i))) {
                            array.add((String) list.get(i));
                        }
                    }
                    break;
                case "Integer":
                    for (int i = 0; i < list.size(); i++) {
                        if (!array.contains((JsonElement) list.get(i))) {
                            array.add((Integer) list.get(i));
                        }
                    }
                    break;
                default:
                    Gson gson = new Gson();
                    for (int i = 0; i < list.size(); i++) {
                        if (!array.contains(gson.toJsonTree(list.get(i)))) {
                            array.add(gson.toJsonTree(list.get(i)));
                        }
                    }
                    break;
            }
            editor.putString(key, array.toString());
            result = true;
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        editor.apply();
        return result;
    }

    /**
     * 获取保存的List
     *
     * @param key key
     * @return 对应的Lis集合
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    public <T> List<T> getListData(String key, Class<T> cls) {
        List<T> list = new ArrayList<>();
        String json = sp.getString(key, "");
        if (!json.equals("") && json.length() > 0) {
            Gson gson = new Gson();
            JsonArray array = new JsonParser().parse(json).getAsJsonArray();
            for (JsonElement elem : array) {
                list.add(gson.fromJson(elem, cls));
            }
        }
        return list;
    }


    public void clearCache() {
//        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MyApp.getApplication());
        sp.edit().clear().apply();
//        SpUtilBase.getInstance().savaBoolean(context, SharedPreferanceKey.ONE_HOME, false);
    }
}
